<template>
  <van-list v-model:loading="loading" :finished="finished" :finished-text="title" @load="getlistFn">
    <slot name="content" :list="list" :loading="loading"></slot>
  </van-list>
</template>

<script setup lang="ts">
const emit = defineEmits(["listData"]);
const props = defineProps(["title", "url", "otherGet"]);
const loading = ref(false);
const listQuery = ref({ size: 10, page: 0 });
const list = ref([]);
const finished = ref(false);
const getlistFn = async () => {
  if (props.otherGet) {
    emit("listData", listFn);
  } else {
    listFn();
  }
};
const listFnNew = () => {
  list.value = [];
  finished.value = false;
  loading.value = true;
  listQuery.value = { size: 10, page: 0 };
  if (props.otherGet) {
    emit("listData", listFn);
  } else {
    listFn();
  }
};
const listFn = async (resd?: any) => {
  let res = resd ? resd : await hfoRequest.get({ url: props.url, params: listQuery.value });
  listQuery.value.size++;
  list.value = list.value.concat(res.data.content);
  loading.value = false;
  let total = res.data.totalElements;
  if (total <= list.value.length) {
    finished.value = true;
  }
  return res.data;
};
defineExpose({
  list,
  loading,
  finished,
  listQuery,
  listFn,
  listFnNew
});
</script>

<style lang="less"></style>
